package com.lsj.core.exception;

import com.lsj.core.constant.exception.ExceptionEnum;
import com.lsj.core.response.BasicResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
*@Author: lin.shj
*@Description: 项目rest请求异常统一拦截，用于返回对前台友好的提示
*/
@RestControllerAdvice
@Slf4j
public class RestGlobalExceptionHandler {

    @ExceptionHandler(CustomerException.class)
    public BasicResponse processException(CustomerException e){
        //这里可以加上写错误日志的操作
        log.error("位置：{}，错误信息：{}",e.getMethod() ,e.getLocalizedMessage());
      return BasicResponse.error(e.getCode(),e.getLocalizedMessage());
    }

    @ExceptionHandler(Exception.class)
    public BasicResponse exception(Exception e){
        //这里可以加上写错误日志的操作
        e.printStackTrace();
        return  BasicResponse.error(ExceptionEnum.UNKNOWN_EXCEPTION.getCode(),e.getMessage());
    }
}
